package com.hospital.controller;

import com.hospital.domain.pojo.AdmissionApplication;
import com.hospital.domain.utils.Result;
import com.hospital.service.AdmissionApplicationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admission")
@CrossOrigin
public class AdmissionController {

    @Autowired
    private AdmissionApplicationService admissionApplicationService;

    /**
     * 提交住院申请
     */
    @PostMapping("/apply")
    public Result<String> submitApplication(@RequestBody AdmissionApplication application) {
        return admissionApplicationService.submitApplication(application);
    }

    /**
     * 获取患者的住院申请记录
     */
    @GetMapping("/patient/{pId}")
    public Result<List<AdmissionApplication>> getPatientApplications(@PathVariable Integer pId) {
        return admissionApplicationService.getPatientApplications(pId);
    }

    /**
     * 获取待审批的住院申请
     */
    @GetMapping("/pending")
    public Result<List<AdmissionApplication>> getPendingApplications() {
        return admissionApplicationService.getPendingApplications();
    }

    /**
     * 获取科室的住院申请
     */
    @GetMapping("/department/{depId}/state/{state}")
    public Result<List<AdmissionApplication>> getDepartmentApplications(
            @PathVariable Integer depId, @PathVariable Integer state) {
        return admissionApplicationService.getDepartmentApplications(depId, state);
    }

    /**
     * 根据医生ID获取住院申请
     */
    @GetMapping("/doctor/{dId}")
    public Result<List<AdmissionApplication>> getDoctorApplications(@PathVariable Integer dId) {
        return admissionApplicationService.getDoctorApplications(dId);
    }

    /**
     * 根据医生ID与状态获取住院申请
     */
    @GetMapping("/doctor/{dId}/state/{state}")
    public Result<List<AdmissionApplication>> getDoctorApplicationsByState(@PathVariable Integer dId,
                                                                          @PathVariable Integer state) {
        return admissionApplicationService.getDoctorApplicationsByState(dId, state);
    }

    /**
     * 批准住院申请
     */
    @PostMapping("/approve/{aId}")
    public Result<String> approveApplication(@PathVariable Integer aId, @RequestParam Integer approveDoctor) {
        return admissionApplicationService.approveApplication(aId, approveDoctor);
    }

    /**
     * 拒绝住院申请
     */
    @PostMapping("/reject/{aId}")
    public Result<String> rejectApplication(@PathVariable Integer aId, 
                                          @RequestParam Integer approveDoctor, 
                                          @RequestParam String rejectReason) {
        return admissionApplicationService.rejectApplication(aId, approveDoctor, rejectReason);
    }

    /**
     * 获取申请详情
     */
    @GetMapping("/info/{aId}")
    public Result<AdmissionApplication> getApplicationById(@PathVariable Integer aId) {
        return admissionApplicationService.getApplicationById(aId);
    }

    /**
     * 获取所有住院申请数据
     */
    @GetMapping("/all")
    public Result<List<AdmissionApplication>> getAllApplications() {
        return admissionApplicationService.getAllApplications();
    }
}
